package com.djinfo.work.service.impl;

import com.djinfo.work.common.bean.AjaxJson;
import com.djinfo.work.common.util.StringUtil;
import com.djinfo.work.dao.QsContentDao;
import com.djinfo.work.entity.QsContentEntity;
import com.djinfo.work.entity.TbSysUsers;
import com.djinfo.work.service.QsContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("qsContentService")
public class QsContentServiceImpl implements QsContentService {
	@Autowired
	private QsContentDao qsContentDao;


	@Override
	public void save(QsContentEntity object) {
		qsContentDao.save(object);
	}

	@Override
	public void update(QsContentEntity object) {
		qsContentDao.update(object);
	}

	@Override
	public List<QsContentEntity> page(QsContentEntity object, Integer pageNum, Integer pageSize) {
		List<QsContentEntity> list=qsContentDao.page(object,pageNum,pageSize);
		return list;
	}

	@Override
	public List<QsContentEntity> list(QsContentEntity object) {
		List<QsContentEntity> list=qsContentDao.page(object,null,null);
		return list;
	}

	@Override
	public Integer getAllRowCount(QsContentEntity object) {
		return qsContentDao.getRowCount(object);
	}

	@Override
	public AjaxJson delete(String id) {
		AjaxJson ajaxJson=new AjaxJson();
		try {
			qsContentDao.delete(id);
		} catch (Exception e) {
			ajaxJson.setCode(301);
			ajaxJson.setMsg("error");
			e.printStackTrace();
		}
		return ajaxJson;
	}


	@Override
	public QsContentEntity selectOne(String id) {
		QsContentEntity qsContentEntity=qsContentDao.selectOne(id);
		return qsContentEntity;
	}

	@Override
	public QsContentEntity getById(String id) {
		QsContentEntity qsContentEntity=qsContentDao.selectOne(id);
		return qsContentEntity;
	}

	@Override
	public AjaxJson list(QsContentEntity qsContentEntity, Integer pagenum, Integer pagesize) {
		AjaxJson ajaxJson=new AjaxJson();
		try {
			HashMap map=new HashMap();
			List<QsContentEntity> list=qsContentDao.page(qsContentEntity,pagenum,pagesize);
			for(QsContentEntity qsContentEntity1:list){
				qsContentEntity1.getTitleId();
			}

			int count=qsContentDao.getRowCount(qsContentEntity);
			map.put("dataList",list);
			map.put("total",count);
			ajaxJson.setResult(map);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ajaxJson;
	}

	@Override
	public AjaxJson saveOrUpdate(QsContentEntity qsContentEntity, HttpServletRequest request) {
		String userId=(String)request.getSession().getAttribute("userId");
		AjaxJson ajaxJson=new AjaxJson();
		String nowTime= StringUtil.getNowTime();
		if(qsContentEntity.getId()==null||"".equals(qsContentEntity.getId())){
			qsContentEntity.setId(null);
			qsContentEntity.setCreateTime(nowTime);
			qsContentEntity.setUpdateTime(nowTime);
			qsContentEntity.setCreateUser(userId);
			qsContentEntity.setUpdateUser(userId);
			qsContentDao.save(qsContentEntity);
		}else{
			qsContentEntity.setUpdateTime(nowTime);
			qsContentEntity.setUpdateUser(userId);
			qsContentDao.update(qsContentEntity);
		}
		return ajaxJson;
	}

    /*获取titleID的内容*/
	@Override
	public List<QsContentEntity> getByTitleId(String id) {

		String hql = "from QsContentEntity where titleId = ? order by order";

		return qsContentDao.getByTitleId(hql,id);
	}
}
